package com.bonc.ioc.util;

import com.bonc.jwt.JwtHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: CurrentUser
 * @Author: Liyy
 * @Description: 获取当前登录用户信息的类, 请根据实际系统进行修改
 * @Date: 2019-12-02 11:43:04
 **/

@Component
@Slf4j
public class CurrentUser {

    public static void main(String[] args) {

        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIiLCJpc05vcm1hbCI6InRydWUiLCJtb2JsaWUiOiIxMzMxMTExMTExMSIsImlzU3lzVGVuYW50IjoidHJ1ZSIsImxvZ2luSWQiOiJhZG1pbiIsIm9yZ3NfanNvbiI6Ilt7XCJpZFwiOlwiQUxMXCIsXCJvcmdOYW1lXCI6XCLlhajlm71cIixcIm9yZ1R5cGVOYW1lXCI6XCLooYzmlL_mnLrmnoRcIixcInBhcmVudElkXCI6XCJcIixcInBhdGhcIjpcIi9BTExcIixcInR5cGVJZFwiOlwiMTExXCJ9XSIsImlzcyI6ImJvbmMiLCJpc0FkbWluIjoidHJ1ZSIsInVzZXJOYW1lIjoi566h55CG5ZGYIiwidXNlcklkIjoiMSIsImF1ZCI6IiIsInRlbmFudE5hbWUiOiLns7vnu5_np5_miLciLCJyb2xlc19qc29uIjoiW3tcImlkXCI6MSxcInJvbGVDb2RlXCI6XCJyb290XCIsXCJyb2xlTmFtZVwiOlwi57O757uf566h55CG5ZGYXCJ9XSIsInRlbmFudElkIjoidGVuYW50X3N5c3RlbSIsImlzTWFuYWdlciI6InRydWUiLCJleHAiOjE2MjA0Nzg0NjEsImVtYWlsIjoiMUAxNjMuY29tIn0.c4dmxHq1YIi3uyKvmZlRyvY8CMGJ_L0FfY2JilAVZm-juOX7qHqDNpAyEoS8VbXBhdHjeon3_rxn9ctad2naB7LGVOxROnKP8N-tlDggXvmGp56fEp3udkv19ufHLq4cAGcLwcOeqBZFCwygpcn80ONyRo3mACyeHsr45saiaweY6rJSgat-sVxIf0Ff_v_mUKUf0bpWUgVBtLAVFqbEaU4KXM81cnkVyA8OFOX-IWFHgc_V485lg4U8Exhwsp5wd-BId7jmY7Jt5b3vokmdQsjsoh8KJCS82_umCUE8GV2BlTgpZt7sDP0Mf_NIEUoFxrRhqp7LNBuzgOZG0gT_CA";
        System.err.println(token + "=============11111111111111111111");
        JwtHelper jwt = new JwtHelper(token);

        System.out.println(jwt.toString());
        String loginId = jwt.getValue("loginId");
        System.out.println(loginId);
    }

    /**
     * 获取当前登录用户id
     *
     * @return
     */
    public String getUserId(HttpServletRequest request) {
        String token = request.getHeader("p-user-token");
        log.info(token + "=============11111111111111111111");
        JwtHelper jwt = new JwtHelper(token);
        String loginId = jwt.getValue("loginId");
        return loginId;
    }

    /**
     * 获取当前登录用户名
     *
     * @return
     */
    public String getUserName(HttpServletRequest request) {
        String tokenId1 = request.getHeader("p-user-token");
        log.info(tokenId1 + "-----------11111111111111111111");
        JwtHelper jwt = new JwtHelper(tokenId1);
        return jwt.getValue("userName");
    }

}
